<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once(ROOT_PATH."class/user.class.php");

if (isset($param['userid'])) {
	$us = new user();
	$us->userid=$param['userid'];
	$ret = $loader->get($us);

	if ($ret) {
		$ret = $us->validate($param['password']);

		if ($ret) {
			if ($us->status==STATUS_INACTIVE) {
				$error=$LANG['userinactive'];	
			} else {
				$_SESSION["s_user"] = $us->userid;
				$loader->execute($us->updateLogin());
				if (isset($param['rememberme'])) {
					setcookie('poat[id]',$us->userid,time()+(COOKIE_EXPIRE*60*60));
					//setcookie('poat[pw]',$us->currentpwd,time()+(COOKIE_EXPIRE*60*60));	
				} else {
					setcookie('poat[id]','',time());
				}
			}
		} else {
			$error = $LANG['invalidcredentials'];
		} 
	} else {
		$error = $LANG['invalidcredentials'];
	}
		
} elseif (isset($_COOKIE['poat']['id']) and isset($_COOKIE['poat']['pw']) and $_COOKIE['poat']['id']!='') {
	//$us = new user();
	//$us->userid=$_COOKIE['poat']['id'];
	//$ret = $loader->get($us);
	// I DISABLED THIS FOR SOME REASON ? I DON'T REMEMBER NOW
	$ret = false;
	if ($ret) {
		$ret = $us->validateNoEncrypt($_COOKIE['poat']['pw']);
		if ($ret) {
			$_SESSION["s_user"] = $us->userid;
			$loader->execute($us->updateLogin());
			setcookie('poat[id]',$us->userid,time()+(COOKIE_EXPIRE*60*60*24),"/");
			//setcookie('poat[pw]',$us->currentpwd,time()+(COOKIE_EXPIRE*60*60*24),"/");	
		}
	}
}

if (!isset($_SESSION["s_user"])) {
	if (isset($_COOKIE['poat']['id'])) {
		$tm->assign('loguser',$_COOKIE['poat']['id']);
	}
	$tm->assign("hidemenu","true");
	$content = $tm->fetch("login.tpl");
} else {
	if (isset($param['redir'])) $action=$param['redir'];
}

?>
